// components/foot-comment/foot-comment.js
import { giveLikeApi, submitComment } from '../../utils/tools.js';
const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        isIphone: Boolean,
        review: String, //是否隐藏 审核字段
        commentcount: Number, //评论数
        commentType: String, //评论类型
        praiseType: String, //点赞类型
        praiseflg: String, //是否点赞
        praisecnt: Number, //点赞数
        placeHoldComment: String, //input显示
        eventId: String,
        tocommentid: String,
        touid: String,
        toreplyid: String,
        privateflg: {
            type: String,
            value: '00',
            observer: function (newVal, oldVal) {
                this.setData({
                    private: newVal === '01',
                });
            },
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        remarkEdit: false,
        show: false,
        anonymous: false, //评论匿名
        remarkValue: '',
        answerBoxBottom: 0,
        private: false, // 私密
    },
    observers: {
        remarkEdit: function (remarkEdit) {
            this.triggerEvent('remarkEditChange', { remarkEdit: remarkEdit });
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        stopEvent(e) {
            return;
        },
        //点赞
        giveLike(e) {
            let _that = this;
            _that.setData({
                praisecnt:
                    _that.data.praiseflg == '00'
                        ? _that.data.praisecnt + 1
                        : _that.data.praisecnt - 1,
                praiseflg: _that.data.praiseflg == '00' ? '01' : '00',
            });
            giveLikeApi(
                _that.data.praiseType,
                _that.data.eventId,
                _that.data.praiseflg
            );
        },
        inputBtn(e) {
            this.setData({
                remarkEdit: true,
            });
        },

        removeFocus() {
            this.setData({
                remarkEdit: false,
                remarkValue: '',
            });
            this.triggerEvent('resetInputContent');
        },

        toggle() {
            if (this.data.privateflg === '01') {
                wx.showToast({
                    title: '回复私密留言只能是私密',
                    icon: 'none',
                    duration: 2000,
                });
                return;
            }
            this.setData({
                // anonymous: !this.data.anonymous,
                private: !this.data.private,
            });
        },
        remarkInput(e) {
            this.setData({
                remarkValue: e.detail.value,
            });
            app.numLimitTip(e.detail.value.length, 200);
        },
        remarkFocus(e) {
            this.triggerEvent('jumpOrRemark', { setplaceHold: e });
            if (e == '01') {
                this.setData(
                    {
                        remarkEdit: true,
                    },
                    () => {
                        wx.onKeyboardHeightChange((res) => {
                            this.setData({
                                answerBoxBottom: res.height,
                            });
                        });
                    }
                );
            } else {
                this.setData({
                    remarkEdit: true,
                    answerBoxBottom: e.detail.height,
                });
            }
        },
        remarkBlur() {
            this.setData({
                // remarkEdit: false,
                answerBoxBottom: 0,
            });
        },
        // 锚点跳转
        jumpOrRemark() {
            this.triggerEvent('jumpOrRemark', { setplaceHold: '00' });
        },
        articalRemark() {
            this.setData({
                remarkEdit: true,
                remarkValue: '', //评论/回复内容
            });
        },
        //提交评论
        submitComment() {
            let _that = this;
            let level = '00';
            if (!_that.data.remarkValue) return;
            if (_that.data.tocommentid) {
                level = '01';
            }
            submitComment(
                _that.data.tocommentid,
                _that.data.eventId,
                _that.data.commentType,
                level,
                _that.data.remarkValue,
                _that.data.anonymous ? '01' : '00',
                _that.data.private ? '01' : '00',
                _that.data.touid,
                _that.data.toreplyid === '' ? null : _that.data.toreplyid
            ).then((res) => {
                if (res.status == 0) {
                    _that.triggerEvent('refreshRemark');
                    _that.triggerEvent('resetInputContent');
                    this.setData({
                        remarkEdit: false,
                        remarkValue: '', //评论/回复内容
                        anonymous: false, //评论匿名
                        private: false,
                    });
                }
            });
        },
    },
});
